<?php
namespace app\admin\controller\contents;
use app\common\controller\Backend;



class Xiaofei extends Backend
{

    protected $relationSearch = false;
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('OrdersInput');
    }

    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach ($list as $k => $v) {
                $v['order_time'] =date("Y-m-d H:i:s",$v['order_time']);
                //$v['good_state'] = $this->model->tpList($v['tp']);
               // $v['sex'] = $this->model->sexList($v['sex']);
                //$v->hidden(['password', 'salt']);
            }
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }

    public function add(){
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a");
            $params = array_filter(array_intersect_key(
                $params,
                array_flip(array('order_sn','kehu_truename','kehu_mobile','order_time'))
            ));
            if(empty($params['order_time']) || strlen($params['order_time'])<5){
                $this->error('请选择订单时间');
            }
            if(empty($params['kehu_mobile']) || strlen($params['kehu_mobile'])<5){
                $this->error('请输入客户手机号码');
            }
            $goods=$_POST['goods'];
            if(empty($goods) || empty($goods[0])){
                $this->error('订单产品不能为空');
            }
            if ($params) {
                $params['order_time']=strtotime($params['order_time']);
                $params['atime']=TIMESTAMP;
                $order_id=Model('orders_input')->insertGetId($params);
                foreach($goods as $v){
                    $insert=array();
                    $insert['order_id']=$order_id;
                    $insert['good_id']=$v;
                    $insert['good_num']=(int)post('num_'.$v);
                    $insert['good_guige']=(int)post('guige_'.$v);
                    $insert['good_price']=(float)post('price_'.$v);
                    Model('orders_input_item')->insert($insert);
                }
                $this->success();
            }
            $this->error();
        }
        $this->view->assign('goods',Model('zidian')->where(array('cid'=>4))->select());
        $this->view->assign('guige',Model('zidian')->where(array('cid'=>33))->select());
        return $this->view->fetch();
    }
    public function edit($ids=NULL){
        if ($this->request->isPost()) {
            $this->token();
            $params = $this->request->post("row/a");
            $params = array_filter(array_intersect_key(
                $params,
                array_flip(array('order_sn','kehu_truename','kehu_mobile','order_time'))
            ));
            if(empty($params['order_time']) || strlen($params['order_time'])<5){
                $this->error('请选择订单时间');
            }
            if(empty($params['kehu_mobile']) || strlen($params['kehu_mobile'])<5){
                $this->error('请输入客户手机号码');
            }
            $goods=$_POST['goods'];
            if(empty($goods) || empty($goods[0])){
                $this->error('订单产品不能为空');
            }
            $info=Model('OrdersInput')->where(array('id'=>$ids))->find();
            if(empty($info)){
                $this->error('订单不存在');
            }
            if ($params) {
                $params['order_time']=strtotime($params['order_time']);
                Model('orders_input')->where(array('id'=>$ids))->update($params);
                foreach($goods as $v){
                    $insert=array();
                    $insert['order_id']=$ids;
                    $insert['good_id']=$v;
                    $insert['good_num']=(int)post('num_'.$v);
                    $insert['good_guige']=(int)post('guige_'.$v);
                    $insert['good_price']=(float)post('price_'.$v);
                    $where=array();
                    $where['order_id']=$ids;
                    $where['good_id']=$v;
                    $have=Model('orders_input_item')->where($where)->find();
                    if(!empty($have)){
                        Model('orders_input_item')->where(array('id'=>$have['id']))->update($insert);
                    }else{
                        Model('orders_input_item')->insert($insert);
                    }
                }
                $this->success();
            }
            $this->error();
        }
        $id=(int)$ids;
        if($id<1){
            $this->error('订单不存在');
        }
        $info=Model('OrdersInput')->where(array('id'=>$id))->find();
        if(empty($info)){
            $this->error('订单不存在');
        }
        $info['order_time']=date("Y-m-d H:i:s",$info['order_time']);
        $goods=Model('OrdersInputItem')->where(array('order_id'=>$id))->select();
        $ginfo=array();
        if(!empty($goods)){
            $gids=array();
            foreach($goods as $v){
                $gids[]=$v['good_id'];
                $ginfo[$v['good_id']]=$v->toArray();
            }
        }else{
            $gids=array();
        }
        $this->view->assign('gids',$gids);
        $this->view->assign('ginfo',$ginfo);
        $this->view->assign('info',$info);
        $this->view->assign('guige',Model('zidian')->where(array('cid'=>33))->select());
        $this->view->assign('goods',Model('zidian')->where(array('cid'=>4))->select());
        return $this->view->fetch();
    }
    public function info($ids=NULL){
        $id=(int)$ids;
        if($id<1){
            $this->error('订单不存在');
        }
        $info=Model('OrdersInput')->where(array('id'=>$id))->find();
        if(empty($info)){
            $this->error('订单不存在');
        }
        $info['order_time']=date("Y-m-d H:i:s",$info['order_time']);
        $goods=Model('OrdersInputItem')->where(array('order_id'=>$id))->select();
        $ginfo=array();
        if(!empty($goods)){
            $gids=array();
            foreach($goods as $v){
                $gids[]=$v['good_id'];
                $ginfo[$v['good_id']]=$v->toArray();
            }
        }else{
            $gids=array();
        }
        $this->view->assign('gids',$gids);
        $this->view->assign('info',$info);
        $this->view->assign('ginfo',$ginfo);
        $this->view->assign('guige',Model('zidian')->where(array('cid'=>33))->select());
        $this->view->assign('goods',Model('zidian')->where(array('cid'=>4))->select());
        return $this->view->fetch();
    }
}
